package pm_StudentManager;

import java.util.ArrayList;
import java.util.Scanner;

public class Manager {
    public static void main(String[] args) {
        //创建集合，用来存储学生数据
        ArrayList<Student> students=new ArrayList<>();
        //用循环完成再次回到主界面
        while (true){
            //打印主菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.");
            System.out.println("6.退出");
            System.out.println("请输入您的选择：");

            //用Scanner来接收用户录入的数据
            Scanner sc=new Scanner(System.in);
            String line = sc.nextLine();
            //用switch完成用户操作的选择
            switch (line){
                case "1":
                    addStudents(students);
//                    System.out.println("添加学生操作");
                    break;

                case "2":
                    adlStudents(students);
//                    System.out.println("删除学生操作");
                    break;
                case "3":
//                    System.out.println("修改学生操作");
                    upperDate(students);
                    break;
                case "4":
//                        System.out.println("查看所有学生操作");
                    foundAll(students);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);//退出JVM，（退出虚拟机）
            }
        }
    }



    //增
    public static void addStudents(ArrayList<Student> array){
        Scanner sc=new Scanner(System.in);
        String sID;
        while (true){
            System.out.println("输入学号");
            sID=sc.nextLine();
            boolean flag=isUsed(array ,sID);
            if (flag==true){
                System.out.println("换一个");
            }else {
                break;
            }
        }
        System.out.println("请输入名字：");
        String name=sc.nextLine();
        System.out.println("请输入年龄：");
        String age=sc.nextLine();
        System.out.println("请输入住址：");
        String home=sc.nextLine();
        Student s=new Student();
        s.setId(sID);
        s.setAge(age);
        s.setHome(home);
        s.setName(name);
        array.add(s);
        //给个添加成功提示
        System.out.println("添加学生成功");
    }
    public static boolean isUsed(ArrayList<Student> array, String sID){

        return false;
    }
    //删
    public static void adlStudents(ArrayList<Student> array){
        //获取键盘输入的
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的学号");
        String sID=sc.nextLine();
        //
        int index=-1;
        for (int i=0;i<array.size();i++){
            Student s=array.get(i);
            if (s.getId().equals(sID)){
                index=i;
                break;
            }
        }
        if (index==-1){
            System.out.println("学生不存在，请重新输入");

        }else {
            array.remove(index);
            System.out.println("删除成功");
        }
    }

    //改
    public static void upperDate(ArrayList<Student> array){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的学生学号");
        String sid = sc.nextLine();
        int index=-1;


        //键盘输入要修改的信息
        System.out.println("请输入新姓名");
        String name=sc.nextLine();
        System.out.println("请输入新年龄");
        String age=sc.nextLine();
        System.out.println("请输入新住址");
        String home=sc.nextLine();
        //创建新学生对象
        Student ss=new Student();
        ss.setId(sid);
        ss.setName(name);
        ss.setAge(age);
        ss.setHome(home);
        //遍历集合修改对应的学生信息
        for (int i=0;i<array.size();i++){
            Student student=array.get(i);
            if (student.getId().equals(sid)){
                array.set(i,ss);
                break;
            }
        }
        System.out.println("修改成功");
    }


//查
    public static void foundAll(ArrayList<Student> array){
        if (array.size()==0){
            System.out.println("没信息别几把查了");
            return;//没信息后面的没必要执行了
        }
        //显示查询结果
        System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");// \t是缩进，相当于tab
        for (int i=0;i<array.size();i++){
            Student s=array.get(i);
            String Sid=s.getId();
            String name=s.getName();
            String age=s.getAge();
            String home=s.getHome();
            System.out.println(Sid+"\t\t"+name+"\t\t"+age+"\t\t"+home);
        }
    }



    static class Student {
        String id;
        String name;
        String age;
        String home;


        public Student() {
        }

        public Student(String id, String name, String age, String home) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.home = home;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getHome() {
            return home;
        }

        public void setHome(String home) {
            this.home = home;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", home='" + home + '\'' +
                    ", id=" + id +
                    '}';
        }
    }

}
